#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>
using namespace std;
#include <boost/thread.hpp>
void p_for()
{
	const int count = 20000000;
	cout << "for ";
	clock_t e = clock();
	for(int i = 0; i < count; i++); 
	e = clock() - e;
	cout << " non parallel cost " << e;

	e = clock();
	#pragma omp parallel for 
	for(int i = 0; i < count; i++);
	e = clock() - e;
	cout << " parallel cost " << e << endl;
}
/*
void parallel_race()
{
	int sum = 0;
	for(int i = 0; i < 5; i++) {
		#pragma omp parallel num_threads()
		{
			int i = omp_get_thread_num();
			//slow down one of them
			if (i == 0) {
				int sum = 0;
				for(int i = 0; i < 900000000; i++) {
					sum++;
				}
			}
			#pragma omp atomic
			{
				sum++;
			}
			printf("thread%d:%d\n", i, sum);
		}
		printf("sum: %d\n", sum);//sum is always even because they come here together
	}
}
*/
void test(int n)
{
	int t = omp_get_thread_num();
	for(int i = 0; i < 99000000; i++) ;//do nothing, just waste time
	cout << n << " from " << t << endl;
}
void run()
{
	cout << "run" << endl;
	#pragma omp parallel for
	for(int i = 0; i < 10; i++)
		test(i);
}
void p_atomic()
{
	int count = 0;
	#pragma omp parallel 
	{
		#pragma omp atomic
		count++;
	}
	cout << "Number of threads:" << count << endl;
}
int main(int argc, char **argv)
{
	//boost::thread t(&run);//it does't work
	//t.join();
	//run();
	p_atomic();
	p_for();
	return 0;
}
